package com.hotel.user.controller;

import com.hotel.common.entity.Permission;
import com.hotel.common.utils.Result;
import com.hotel.user.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/permissions")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/{id}")
    public Result<Permission> getPermissionById(@PathVariable("id") Long id) {
        return permissionService.getPermissionById(id);
    }

    @GetMapping("/code/{permissionCode}")
    public Result<Permission> getPermissionByCode(@PathVariable("permissionCode") String permissionCode) {
        return permissionService.getPermissionByCode(permissionCode);
    }

    @GetMapping
    public Result<List<Permission>> getAllPermissions() {
        return permissionService.getAllPermissions();
    }

    @GetMapping("/role/{roleId}")
    public Result<List<Permission>> getPermissionsByRoleId(@PathVariable("roleId") Long roleId) {
        return permissionService.getPermissionsByRoleId(roleId);
    }

    @GetMapping("/user/{userId}")
    public Result<List<Permission>> getPermissionsByUserId(@PathVariable("userId") Long userId) {
        return permissionService.getPermissionsByUserId(userId);
    }

    @PostMapping
    public Result<Long> createPermission(@RequestBody Permission permission) {
        return permissionService.createPermission(permission);
    }

    @PutMapping
    public Result<Void> updatePermission(@RequestBody Permission permission) {
        return permissionService.updatePermission(permission);
    }

    @DeleteMapping("/{id}")
    public Result<Void> deletePermission(@PathVariable("id") Long id) {
        return permissionService.deletePermission(id);
    }
}